from django.db.utils import *
from django.db import connection, connections
from importlib import import_module


def run():
    alias = 'default'
    databases = {'default': {'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_core_code', 'USER': 'root',
                             'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306', 'ATOMIC_REQUESTS': False,
                             'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'OPTIONS': {}, 'TIME_ZONE': None,
                             'TEST': {'CHARSET': None, 'COLLATION': None, 'MIGRATE': True, 'MIRROR': None,
                                      'NAME': None}},
                 'test': {'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_core_code', 'USER': 'root',
                          'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306'}}
    conn = databases[alias]
    conn.setdefault('ATOMIC_REQUESTS', False)
    conn.setdefault('AUTOCOMMIT', True)
    conn.setdefault('ENGINE', 'django.db.backends.dummy')
    if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
        conn['ENGINE'] = 'django.db.backends.dummy'
    conn.setdefault('CONN_MAX_AGE', 0)
    conn.setdefault('OPTIONS', {})
    conn.setdefault('TIME_ZONE', None)
    for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
        conn.setdefault(setting, '')
    db = conn
    backend_name = db['ENGINE']
    backend = import_module('%s.base' % backend_name)
    database_wrapper = backend.DatabaseWrapper(db, alias)
    cursor = database_wrapper.cursor()
    cursor.execute(f'SELECT id FROM books')
    print(cursor.fetchall())
    print(cursor)

